package com.label.Service.Impl;

import com.label.Dao.ProfilesDisMapper;
import com.label.Model.Query.ProfileDisQuery;
import com.label.Model.Vo.ProfileDisVo;
import com.label.Service.ProfilesDisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ProfilesDisServiceImpl implements ProfilesDisService {

    @Autowired
    ProfilesDisMapper profilesDisMapper;

    @Override
    public List<ProfileDisVo> selectAllProfileSDis(ProfileDisQuery profileDisQuery) {

        List<Map<String, Object>> list = profilesDisMapper.selectAllProfileSDis(profileDisQuery);
        //拼接成前段需要的格式：
        List<ProfileDisVo> profileDisList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {

            int j = i;
            ProfileDisVo profileDisVoTemp = new ProfileDisVo();

            while (j < list.size() && list.get(j).get("accountName").toString().equals(list.get(i).get("accountName").toString())) {

                if (i == j) {
                    profileDisVoTemp.setAccountId(list.get(i).get("accountId").toString());
                    profileDisVoTemp.setAccountName(list.get(i).get("accountName").toString());
                    profileDisVoTemp.setRoleName(list.get(i).get("roleName").toString());
                    if (list.get(i).containsKey("profileName")) {
                        profileDisVoTemp.setProfileName(list.get(i).get("profileName").toString());
                    } else {
                        profileDisVoTemp.setProfileName("");
                    }

                } else {
                    String tempRoleName = profileDisVoTemp.getRoleName();
                    tempRoleName += ("_" + list.get(j).get("roleName").toString());
                    profileDisVoTemp.setRoleName(tempRoleName);

                    String tempProfileName = profileDisVoTemp.getProfileName();
                    if (list.get(j).containsKey("profileName")) {
                        tempProfileName += ("_" + list.get(j).get("profileName").toString());
                    }

                    profileDisVoTemp.setProfileName(tempProfileName);
                }
                j++;
            }
            profileDisList.add(profileDisVoTemp);
            i = j - 1;
        }
        return profileDisList;
    }

    @Override
    public List<Map<String, Object>> getAllRole(String accountId) {
        return profilesDisMapper.getAllRole(accountId);
    }

    @Override
    public List<Map<String, String>> getAllProfiles(String accountId, String roleId) {
        List<Map<String, String>> list = profilesDisMapper.getAllProfiles(accountId, roleId);
        for (int l = 0; l < list.size(); l++) {
            //当查询到一条数据，但是map中字段为null时，list长度为1
            Map<String, String> mapTemp = list.get(l);
            if (mapTemp != null && mapTemp.containsKey("profileId")) {
                list.get(l).put("key", list.get(l).get("profileId"));
            }
        }
        return list;
    }

    @Override
    public List<Map<String, String>> getUnusedProfiles(String accountId, String roleId, String profileId) {
        List<Map<String, String>> list = profilesDisMapper.getUnusedProfiles(accountId, roleId, profileId);
        for (int l = 0; l < list.size(); l++) {
            Map<String, String> mapTemp = list.get(l);
            if (mapTemp != null && mapTemp.containsKey("profileId")) {
                list.get(l).put("key", list.get(l).get("profileId"));
            }
        }
        return list;
    }

    @Override
    public Boolean insertProfile(String accountId, String roleId, String profileId) {
        return profilesDisMapper.insertProfile(accountId, roleId, profileId);
    }

    @Override
    public Boolean delProfile(String accountId, String roleId, String profileId) {
        return profilesDisMapper.delProfile(accountId, roleId, profileId);
    }

}
